function pdf24_trim(str) {
	return str.replace (/^\s+/, '').replace (/\s+$/, '');
}
function pdf24_hasClassName(classNameStr, className) {
	var classNames = classNameStr.split(" ");
	for(var i=0; i<classNames.length; i++) {
		if(classNames[i] == className) {
			return true;
		}
	}
	return false;
}
function pdf24_find(parent, tagName, className) {
	var res = new Array();
	var nodes = parent.getElementsByTagName(tagName);
	for(var i=0; i<nodes.length; i++) {
		if(pdf24_hasClassName(nodes[i].className, className)) {
			res.push(nodes[i]);
		}
	}
	return res;
}
function pdf24_getBlogPosts() {
	var posts = pdf24_find(document.body, "div", "post");
	return posts;
}
function pdf24_getPostData(post) {
	var res;
	var obj = new Object();
	
	//title + link
	obj.link = obj.title = '';
	var h3s = post.getElementsByTagName("h3");
	if(h3s && h3s.length > 0) {
		var h3 = h3s[0];
		var as = h3.getElementsByTagName("a");
		if(as && as.length > 0) {
			var a = as[0];
			obj.link = a.href;
			obj.title = a.innerHTML;
		}
	}
	
	//body
	obj.body = "";
	res = pdf24_find(post, "div", "post-body");
	if(res.length > 0) {
		obj.body = res[0].innerHTML;
	}
		
	//by
	obj.by = "";
	res = pdf24_find(post, "span", "fn");
	if(res.length > 0) {
		obj.by = res[0].innerHTML;
	}
	return obj;
}
function pdf24_getBlogParams() {	
	var doc = document;
	var obj = new Object();
		
	//url
	obj.url = doc.location.href;
			
	//charset
	obj.charset = "UTF-8";
	var metas = doc.getElementsByTagName("meta");
	for(var i=0; i<metas.length; i++) {
		if(metas[i].httpEquiv == "content-type") {
			var res = metas[i].content.match(/charset=(.+)/i);
			if(res) {
				obj.charset = res[1];
			}
			break;
		}
	}		
	
	//name
	obj.name = "";
	var h1s = pdf24_find(doc, "h1", "title");
	if(h1s.length > 0) {
		var h1 = h1s[0];
		var as = h1.getElementsByTagName("a");
		if(as && as.length > 0) {
			obj.name = pdf24_trim(as[0].innerHTML);
		} else {
			obj.name = pdf24_trim(h1.innerHTML);
		}
	}
	return obj;
}
function pdf24_htmlspecialchars(str) {
	var from=new Array(/&/g,/</g,/>/g,/"/g,/'/g);
	var to=new Array("&amp;","&lt;","&gt;","&#039;","&quot;");
	for(var i in from) str=str.replace(from[i],to[i]);
	return str;
}
function pdf24_appendHiddenField(parent, name, value) {
	var field = document.createElement("input");
	field.type = "hidden";
	field.name = name;
	field.value = value;
	parent.appendChild(field);
}

var field = document.createElement("input");
	field.type = "hidden";
	field.name = name;
	field.value = value;
	parent.appendChild(field);

function pdf24_createFields(form) {
	var posts = pdf24_getBlogPosts();
	if(posts.length == 0) {
		return 0;
	}
	var container = document.createElement("div");
	container.id = "pdf24Container";
	form.appendChild(container);	
	for(var i=0; i<posts.length; i++) {
		var postData = pdf24_getPostData(posts[i]);
		//alert(postData.link + "\n" + postData.title + "\n" + postData.body + "\n" + postData.by);
		pdf24_appendHiddenField(container, "postTitle_" + i, postData.title);
		pdf24_appendHiddenField(container, "postLink_" + i, postData.link);
		pdf24_appendHiddenField(container, "postAuthor_" + i, "");
		pdf24_appendHiddenField(container, "postDateTime_" + i, "");
		pdf24_appendHiddenField(container, "postContent_" + i, (postData.body));
	}
	pdf24_appendHiddenField(container, "blogPosts", posts.length);			
	var blogParams = pdf24_getBlogParams();
	//alert(blogParams.charset + "\n" + blogParams.url + "\n" + blogParams.name);
	pdf24_appendHiddenField(container, "blogCharset", blogParams.charset);
	pdf24_appendHiddenField(container, "blogUrl", blogParams.url);
	pdf24_appendHiddenField(container, "blogName", blogParams.name);
	pdf24_appendHiddenField(container, "blogValueEncoding", "htmlSpecialChars");	
	
	//alert(container.innerHTML);
	return posts.length;
}
function pdf24_removeOldContainer() {
	var container = document.getElementById("pdf24Container");
	if(container) {
		container.parentNode.removeChild(container);
	}
}
function pdf24_onSubmitBtnClick() {
	pdf24_removeOldContainer();
	if(pdf24_createFields(document.pdf24Form) == 0) {
		alert("Couldn't find any articles!");
	}
}
function pdf24_onSubmit(form) {
	//return false;
	if(document.getElementById("pdf24Container")) {
		var win = window.open('about:blank', 'pdf24PopWin', 'resizable=yes,scrollbars=yes,width=400,height=200,top=0,left=0');
		return true;
	}
	return false;
}